package com.wjjun.viewmodel;

import android.databinding.Observable;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.wjjun.basemvpvm.BR;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;
import com.wjjun.presenter.ViewGroupPresenter;

import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 列表等使用的viewModel
 * Created by 67535 on 2018/4/29.
 */
@SuppressWarnings("ConstantConditions")
public class CommListViewModel extends BaseViewModel {

    //数据源
    public final ObservableField<ObservableList> items = new ObservableField<>();

    //绑定关系,多类型item就是通过这个实现的
    public final ObservableField<ItemBinding> itemBinding = new ObservableField<>();

    //是否显示分割线
    public final ObservableField<Boolean> divider = new ObservableField<>(false);

    //定义不同的状态
    public final ObservableField<Integer> state = new ObservableField<>(0);
    public final ObservableField<StateViewModel> stateViewModel = new ObservableField<>(new StateViewModel());

    //是否可以下拉刷新
    public final ObservableField<Boolean> enableRefresh = new ObservableField<>(true);
    //是否可以上拉加载
    public final ObservableField<Boolean> enableLoadMore = new ObservableField<>(true);

    public final ObservableField<Integer> spanCount = new ObservableField<>(2);

    public final ObservableField<Integer> orientation = new ObservableField<>(LinearLayoutManager.VERTICAL);

    public final ObservableField<GridLayoutManager.SpanSizeLookup> spanSizeLookup = new ObservableField<>();

    public Object lastItem;

    /**
     * 单类型时使用
     */
    public CommListViewModel(ObservableList<? extends Observable> itemsVal, int layoutId, ViewGroupPresenter presenter) {
        itemBinding.set(ItemBinding.of(BR.viewModel, layoutId).bindExtra(BR.presenter, presenter));
        items.set(itemsVal);
        addChangeListener();
    }

    /**
     * 多类型item时使用
     */
    public CommListViewModel(ObservableList<? extends Observable> itemsVal, OnItemBind onItemBind) {
        itemBinding.set(ItemBinding.of(onItemBind));
        items.set(itemsVal);
        addChangeListener();
    }

    @SuppressWarnings("unchecked")
    private void addChangeListener() {
        if (items.get() != null) {
            items.get().addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList>() {
                @Override
                public void onChanged(ObservableList observableList) {
                    onDataChange(observableList);
                }

                @Override
                public void onItemRangeChanged(ObservableList observableList, int i, int i1) {
                    onDataChange(observableList);
                }

                @Override
                public void onItemRangeInserted(ObservableList observableList, int i, int i1) {
                    onDataChange(observableList);
                }

                @Override
                public void onItemRangeMoved(ObservableList observableList, int i, int i1, int i2) {
                    onDataChange(observableList);
                }

                @Override
                public void onItemRangeRemoved(ObservableList observableList, int i, int i1) {
                    onDataChange(observableList);
                }
            });
        }
    }

    private void onDataChange(ObservableList observableList) {
        if (observableList.isEmpty()) {
            showEmpty();
            lastItem = null;
        } else {
            showSuccess();
            lastItem = items.get().get(items.get().size() - 1);
        }
    }

    public void setSpanSizeLookup(GridLayoutManager.SpanSizeLookup spanSizeLookup){
        this.spanSizeLookup.set(spanSizeLookup);
    }


    public void showDivider() {
        divider.set(true);
    }

    public void setEnableLoadMore(boolean enable) {
        this.enableLoadMore.set(enable);
    }

    public void setEnableRefresh(boolean enable) {
        this.enableRefresh.set(enable);
    }

    /**
     * 显示Loading状态
     */
    public void showLoading() {
        if (this.state.get() != 0) {
            this.state.set(0);
        }
    }


    /**
     * 显示成功状态
     */
    public void showSuccess() {
        if (this.state.get() != 1) {
            this.state.set(1);
        }
    }

    /**
     * 显示无数据状态
     */
    public void showEmpty() {
        if (this.state.get() != 2) {
            this.state.set(2);
        }
    }

    public void setEmpty(String msg, int resId) {
        this.stateViewModel.get().setEmptyText(msg);
        this.stateViewModel.get().setEmptyImg(resId);
    }

    public void setError(String msg) {
        this.stateViewModel.get().setNetErrorText(msg);
    }

    /**
     * 显示请求错误状态
     */
    public void showError() {
        if (this.state.get() != 3) {
            this.state.set(3);
        }
    }

    /**
     * 显示登录状态
     */
    public void showLogin() {
        if (this.state.get() != 4) {
            this.state.set(4);
        }
    }

    /**
     * 最后的数据item， 用于分页
     *
     * @return
     */
    public Object getLastItem() {
        return lastItem;
    }

    /**
     * 判断是否错误界面
     */
    public boolean isErrorPage() {
        if (state.get() == 2 || state.get() == 3 || state.get() == 4) {
            return true;
        }
        return false;
    }

    public boolean isLoadingPage(){
        if( state.get() == 0 ){
            return true;
        }
        return false;
    }

    public int getSpanCount(){
        if( spanCount == null){
            return 0;
        }else {
            return spanCount.get();
        }
    }
}
